package com.ruoyi.web.controller.crm;

import com.bonuo.crm.domain.Device;
import com.bonuo.crm.service.DeviceService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//设备Controller
@RestController
@RequestMapping("/crm/device")
@Api(tags = "项目所属设备")
public class DeviceController extends BaseController {
    @Autowired
    private DeviceService deviceService;

    //查询设备列表
    @ApiOperation(value = "查询设备列表",httpMethod = "GET")
    @GetMapping("/list")
    public AjaxResult list(Device device) {
        List<Device> deviceList = deviceService.selectDeviceList(device);
        // 查询设备总金额
        BigDecimal amount = deviceService.selectAmount(device);
        //查询该项目下设备金额
        BigDecimal bidAmount = deviceService.selectBidAmount(device.getProjectFundId());
        // 封装数据
        Map<String, Object> map = new HashMap<>();
        map.put("list", getDataTable(deviceList));
        map.put("amount", amount);
        map.put("bidAmount", bidAmount);
        return success(map);
    }

    //添加设备
    @ApiOperation(value = "添加设备",httpMethod = "POST")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Device device) {
        Integer add = deviceService.insertDevice(device);
        return toAjax(add);
    }

    //删除设备
    @ApiOperation(value = "删除设备",httpMethod = "DELETE")
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Integer id) {
        Integer delete = deviceService.deleteDeviceById(id);
        return toAjax(delete);
    }

    //修改设备
    @ApiOperation(value = "修改设备",httpMethod = "PUT")
    @PutMapping("/update")
    public AjaxResult update(@RequestBody Device device) {
        Integer update = deviceService.updateDevice(device);
        return toAjax(update);
    }

    //导出设备
    @PostMapping("/export")
    @ApiOperation(value = "导出设备",httpMethod = "POST")
    public void export(HttpServletResponse response, Device device) {
        List<Device> list = deviceService.selectDeviceList(device);
        ExcelUtil<Device> util = new ExcelUtil<Device>(Device.class);
        util.exportExcel(response, list, "设备数据");
    }

}
